1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.StringG; 26 27 private import glib.Bytes; 28 private import glib.ConstructionException; 29 private import glib.Str; 30 private import glib.c.functions; 31 public import glib.c.types; 32 33 34 /** 35 * The GString struct contains the public fields of a GString. 36 */ 37 public class StringG 38 { 39 /** the main Gtk struct */ 40 protected GString* gString; 41 protected bool ownedRef; 42 43 /** Get the main Gtk struct */ 44 public GString* getStringGStruct(bool transferOwnership = false) 45 { 46 if (transferOwnership) 47 ownedRef = false; 48 return gString; 49 } 50 51 /** the main Gtk struct as a void* */ 52 protected void* getStruct() 53 { 54 return cast(void*)gString; 55 } 56 57 /** 58 * Sets our main struct and passes it to the parent class. 59 */ 60 public this (GString* gString, bool ownedRef = false) 61 { 62 this.gString = gString; 63 this.ownedRef = ownedRef; 64 } 65 66 67 /** 68 * Creates a new #GString, initialized with the given string. 69 * 70 * Params: 71 * init = the initial text to copy into the string, or %NULL to 72 * start with an empty string 73 * 74 * Returns: the new #GString 75 * 76 * Throws: ConstructionException GTK+ fails to create the object. 77 */ 78 public this(string init) 79 { 80 auto __p = g_string_new(Str.toStringz(init)); 81 82 if(__p is null) 83 { 84 throw new ConstructionException("null returned by new"); 85 } 86 87 this(cast(GString*) __p); 88 } 89 90 /** 91 * Creates a new #GString with @len bytes of the @init buffer. 92 * Because a length is provided, @init need not be nul-terminated, 93 * and can contain embedded nul bytes. 94 * 95 * Since this function does not stop at nul bytes, it is the caller's 96 * responsibility to ensure that @init has at least @len addressable 97 * bytes. 98 * 99 * Params: 100 * init = initial contents of the string 101 * len = length of @init to use 102 * 103 * Returns: a new #GString 104 * 105 * Throws: ConstructionException GTK+ fails to create the object. 106 */ 107 public this(string init, ptrdiff_t len) 108 { 109 auto __p = g_string_new_len(Str.toStringz(init), len); 110 111 if(__p is null) 112 { 113 throw new ConstructionException("null returned by new_len"); 114 } 115 116 this(cast(GString*) __p); 117 } 118 119 /** 120 * Creates a new #GString, with enough space for @dfl_size 121 * bytes. This is useful if you are going to add a lot of 122 * text to the string and don't want it to be reallocated 123 * too often. 124 * 125 * Params: 126 * dflSize = the default size of the space allocated to hold the string 127 * 128 * Returns: the new #GString 129 * 130 * Throws: ConstructionException GTK+ fails to create the object. 131 */ 132 public this(size_t dflSize) 133 { 134 auto __p = g_string_sized_new(dflSize); 135 136 if(__p is null) 137 { 138 throw new ConstructionException("null returned by sized_new"); 139 } 140 141 this(cast(GString*) __p); 142 } 143 144 /** 145 * Adds a string onto the end of a #GString, expanding 146 * it if necessary. 147 * 148 * Params: 149 * val = the string to append onto the end of @string 150 * 151 * Returns: @string 152 */ 153 public StringG append(string val) 154 { 155 auto __p = g_string_append(gString, Str.toStringz(val)); 156 157 if(__p is null) 158 { 159 return null; 160 } 161 162 return new StringG(cast(GString*) __p); 163 } 164 165 /** 166 * Adds a byte onto the end of a #GString, expanding 167 * it if necessary. 168 * 169 * Params: 170 * c = the byte to append onto the end of @string 171 * 172 * Returns: @string 173 */ 174 public StringG appendC(char c) 175 { 176 auto __p = g_string_append_c(gString, c); 177 178 if(__p is null) 179 { 180 return null; 181 } 182 183 return new StringG(cast(GString*) __p); 184 } 185 186 /** 187 * Appends @len bytes of @val to @string. 188 * 189 * If @len is positive, @val may contain embedded nuls and need 190 * not be nul-terminated. It is the caller's responsibility to 191 * ensure that @val has at least @len addressable bytes. 192 * 193 * If @len is negative, @val must be nul-terminated and @len 194 * is considered to request the entire string length. This 195 * makes g_string_append_len() equivalent to g_string_append(). 196 * 197 * Params: 198 * val = bytes to append 199 * len = number of bytes of @val to use, or -1 for all of @val 200 * 201 * Returns: @string 202 */ 203 public StringG appendLen(string val, ptrdiff_t len) 204 { 205 auto __p = g_string_append_len(gString, Str.toStringz(val), len); 206 207 if(__p is null) 208 { 209 return null; 210 } 211 212 return new StringG(cast(GString*) __p); 213 } 214 215 /** 216 * Converts a Unicode character into UTF-8, and appends it 217 * to the string. 218 * 219 * Params: 220 * wc = a Unicode character 221 * 222 * Returns: @string 223 */ 224 public StringG appendUnichar(dchar wc) 225 { 226 auto __p = g_string_append_unichar(gString, wc); 227 228 if(__p is null) 229 { 230 return null; 231 } 232 233 return new StringG(cast(GString*) __p); 234 } 235 236 /** 237 * Appends @unescaped to @string, escaping any characters that 238 * are reserved in URIs using URI-style escape sequences. 239 * 240 * Params: 241 * unescaped = a string 242 * reservedCharsAllowed = a string of reserved characters allowed 243 * to be used, or %NULL 244 * allowUtf8 = set %TRUE if the escaped string may include UTF8 characters 245 * 246 * Returns: @string 247 * 248 * Since: 2.16 249 */ 250 public StringG appendUriEscaped(string unescaped, string reservedCharsAllowed, bool allowUtf8) 251 { 252 auto __p = g_string_append_uri_escaped(gString, Str.toStringz(unescaped), Str.toStringz(reservedCharsAllowed), allowUtf8); 253 254 if(__p is null) 255 { 256 return null; 257 } 258 259 return new StringG(cast(GString*) __p); 260 } 261 262 /** 263 * Appends a formatted string onto the end of a #GString. 264 * This function is similar to g_string_append_printf() 265 * except that the arguments to the format string are passed 266 * as a va_list. 267 * 268 * Params: 269 * format = the string format. See the printf() documentation 270 * args = the list of arguments to insert in the output 271 * 272 * Since: 2.14 273 */ 274 public void appendVprintf(string format, void* args) 275 { 276 g_string_append_vprintf(gString, Str.toStringz(format), args); 277 } 278 279 /** 280 * Converts all uppercase ASCII letters to lowercase ASCII letters. 281 * 282 * Returns: passed-in @string pointer, with all the 283 * uppercase characters converted to lowercase in place, 284 * with semantics that exactly match g_ascii_tolower(). 285 */ 286 public StringG asciiDown() 287 { 288 auto __p = g_string_ascii_down(gString); 289 290 if(__p is null) 291 { 292 return null; 293 } 294 295 return new StringG(cast(GString*) __p); 296 } 297 298 /** 299 * Converts all lowercase ASCII letters to uppercase ASCII letters. 300 * 301 * Returns: passed-in @string pointer, with all the 302 * lowercase characters converted to uppercase in place, 303 * with semantics that exactly match g_ascii_toupper(). 304 */ 305 public StringG asciiUp() 306 { 307 auto __p = g_string_ascii_up(gString); 308 309 if(__p is null) 310 { 311 return null; 312 } 313 314 return new StringG(cast(GString*) __p); 315 } 316 317 /** 318 * Copies the bytes from a string into a #GString, 319 * destroying any previous contents. It is rather like 320 * the standard strcpy() function, except that you do not 321 * have to worry about having enough space to copy the string. 322 * 323 * Params: 324 * rval = the string to copy into @string 325 * 326 * Returns: @string 327 */ 328 public StringG assign(string rval) 329 { 330 auto __p = g_string_assign(gString, Str.toStringz(rval)); 331 332 if(__p is null) 333 { 334 return null; 335 } 336 337 return new StringG(cast(GString*) __p); 338 } 339 340 /** 341 * Converts a #GString to lowercase. 342 * 343 * Deprecated: This function uses the locale-specific 344 * tolower() function, which is almost never the right thing. 345 * Use g_string_ascii_down() or g_utf8_strdown() instead. 346 * 347 * Returns: the #GString 348 */ 349 public StringG down() 350 { 351 auto __p = g_string_down(gString); 352 353 if(__p is null) 354 { 355 return null; 356 } 357 358 return new StringG(cast(GString*) __p); 359 } 360 361 /** 362 * Compares two strings for equality, returning %TRUE if they are equal. 363 * For use with #GHashTable. 364 * 365 * Params: 366 * v2 = another #GString 367 * 368 * Returns: %TRUE if the strings are the same length and contain the 369 * same bytes 370 */ 371 public bool equal(StringG v2) 372 { 373 return g_string_equal(gString, (v2 is null) ? null : v2.getStringGStruct()) != 0; 374 } 375 376 /** 377 * Removes @len bytes from a #GString, starting at position @pos. 378 * The rest of the #GString is shifted down to fill the gap. 379 * 380 * Params: 381 * pos = the position of the content to remove 382 * len = the number of bytes to remove, or -1 to remove all 383 * following bytes 384 * 385 * Returns: @string 386 */ 387 public StringG erase(ptrdiff_t pos, ptrdiff_t len) 388 { 389 auto __p = g_string_erase(gString, pos, len); 390 391 if(__p is null) 392 { 393 return null; 394 } 395 396 return new StringG(cast(GString*) __p); 397 } 398 399 /** 400 * Frees the memory allocated for the #GString. 401 * If @free_segment is %TRUE it also frees the character data. If 402 * it's %FALSE, the caller gains ownership of the buffer and must 403 * free it after use with g_free(). 404 * 405 * Params: 406 * freeSegment = if %TRUE, the actual character data is freed as well 407 * 408 * Returns: the character data of @string 409 * (i.e. %NULL if @free_segment is %TRUE) 410 */ 411 public string free(bool freeSegment) 412 { 413 auto retStr = g_string_free(gString, freeSegment); 414 415 scope(exit) Str.freeString(retStr); 416 return Str.toString(retStr); 417 } 418 419 /** 420 * Transfers ownership of the contents of @string to a newly allocated 421 * #GBytes. The #GString structure itself is deallocated, and it is 422 * therefore invalid to use @string after invoking this function. 423 * 424 * Note that while #GString ensures that its buffer always has a 425 * trailing nul character (not reflected in its "len"), the returned 426 * #GBytes does not include this extra nul; i.e. it has length exactly 427 * equal to the "len" member. 428 * 429 * Returns: A newly allocated #GBytes containing contents of @string; @string itself is freed 430 * 431 * Since: 2.34 432 */ 433 public Bytes freeToBytes() 434 { 435 auto __p = g_string_free_to_bytes(gString); 436 437 if(__p is null) 438 { 439 return null; 440 } 441 442 return new Bytes(cast(GBytes*) __p, true); 443 } 444 445 /** 446 * Creates a hash code for @str; for use with #GHashTable. 447 * 448 * Returns: hash code for @str 449 */ 450 public uint hash() 451 { 452 return g_string_hash(gString); 453 } 454 455 /** 456 * Inserts a copy of a string into a #GString, 457 * expanding it if necessary. 458 * 459 * Params: 460 * pos = the position to insert the copy of the string 461 * val = the string to insert 462 * 463 * Returns: @string 464 */ 465 public StringG insert(ptrdiff_t pos, string val) 466 { 467 auto __p = g_string_insert(gString, pos, Str.toStringz(val)); 468 469 if(__p is null) 470 { 471 return null; 472 } 473 474 return new StringG(cast(GString*) __p); 475 } 476 477 /** 478 * Inserts a byte into a #GString, expanding it if necessary. 479 * 480 * Params: 481 * pos = the position to insert the byte 482 * c = the byte to insert 483 * 484 * Returns: @string 485 */ 486 public StringG insertC(ptrdiff_t pos, char c) 487 { 488 auto __p = g_string_insert_c(gString, pos, c); 489 490 if(__p is null) 491 { 492 return null; 493 } 494 495 return new StringG(cast(GString*) __p); 496 } 497 498 /** 499 * Inserts @len bytes of @val into @string at @pos. 500 * 501 * If @len is positive, @val may contain embedded nuls and need 502 * not be nul-terminated. It is the caller's responsibility to 503 * ensure that @val has at least @len addressable bytes. 504 * 505 * If @len is negative, @val must be nul-terminated and @len 506 * is considered to request the entire string length. 507 * 508 * If @pos is -1, bytes are inserted at the end of the string. 509 * 510 * Params: 511 * pos = position in @string where insertion should 512 * happen, or -1 for at the end 513 * val = bytes to insert 514 * len = number of bytes of @val to insert, or -1 for all of @val 515 * 516 * Returns: @string 517 */ 518 public StringG insertLen(ptrdiff_t pos, string val, ptrdiff_t len) 519 { 520 auto __p = g_string_insert_len(gString, pos, Str.toStringz(val), len); 521 522 if(__p is null) 523 { 524 return null; 525 } 526 527 return new StringG(cast(GString*) __p); 528 } 529 530 /** 531 * Converts a Unicode character into UTF-8, and insert it 532 * into the string at the given position. 533 * 534 * Params: 535 * pos = the position at which to insert character, or -1 536 * to append at the end of the string 537 * wc = a Unicode character 538 * 539 * Returns: @string 540 */ 541 public StringG insertUnichar(ptrdiff_t pos, dchar wc) 542 { 543 auto __p = g_string_insert_unichar(gString, pos, wc); 544 545 if(__p is null) 546 { 547 return null; 548 } 549 550 return new StringG(cast(GString*) __p); 551 } 552 553 /** 554 * Overwrites part of a string, lengthening it if necessary. 555 * 556 * Params: 557 * pos = the position at which to start overwriting 558 * val = the string that will overwrite the @string starting at @pos 559 * 560 * Returns: @string 561 * 562 * Since: 2.14 563 */ 564 public StringG overwrite(size_t pos, string val) 565 { 566 auto __p = g_string_overwrite(gString, pos, Str.toStringz(val)); 567 568 if(__p is null) 569 { 570 return null; 571 } 572 573 return new StringG(cast(GString*) __p); 574 } 575 576 /** 577 * Overwrites part of a string, lengthening it if necessary. 578 * This function will work with embedded nuls. 579 * 580 * Params: 581 * pos = the position at which to start overwriting 582 * val = the string that will overwrite the @string starting at @pos 583 * len = the number of bytes to write from @val 584 * 585 * Returns: @string 586 * 587 * Since: 2.14 588 */ 589 public StringG overwriteLen(size_t pos, string val, ptrdiff_t len) 590 { 591 auto __p = g_string_overwrite_len(gString, pos, Str.toStringz(val), len); 592 593 if(__p is null) 594 { 595 return null; 596 } 597 598 return new StringG(cast(GString*) __p); 599 } 600 601 /** 602 * Adds a string on to the start of a #GString, 603 * expanding it if necessary. 604 * 605 * Params: 606 * val = the string to prepend on the start of @string 607 * 608 * Returns: @string 609 */ 610 public StringG prepend(string val) 611 { 612 auto __p = g_string_prepend(gString, Str.toStringz(val)); 613 614 if(__p is null) 615 { 616 return null; 617 } 618 619 return new StringG(cast(GString*) __p); 620 } 621 622 /** 623 * Adds a byte onto the start of a #GString, 624 * expanding it if necessary. 625 * 626 * Params: 627 * c = the byte to prepend on the start of the #GString 628 * 629 * Returns: @string 630 */ 631 public StringG prependC(char c) 632 { 633 auto __p = g_string_prepend_c(gString, c); 634 635 if(__p is null) 636 { 637 return null; 638 } 639 640 return new StringG(cast(GString*) __p); 641 } 642 643 /** 644 * Prepends @len bytes of @val to @string. 645 * 646 * If @len is positive, @val may contain embedded nuls and need 647 * not be nul-terminated. It is the caller's responsibility to 648 * ensure that @val has at least @len addressable bytes. 649 * 650 * If @len is negative, @val must be nul-terminated and @len 651 * is considered to request the entire string length. This 652 * makes g_string_prepend_len() equivalent to g_string_prepend(). 653 * 654 * Params: 655 * val = bytes to prepend 656 * len = number of bytes in @val to prepend, or -1 for all of @val 657 * 658 * Returns: @string 659 */ 660 public StringG prependLen(string val, ptrdiff_t len) 661 { 662 auto __p = g_string_prepend_len(gString, Str.toStringz(val), len); 663 664 if(__p is null) 665 { 666 return null; 667 } 668 669 return new StringG(cast(GString*) __p); 670 } 671 672 /** 673 * Converts a Unicode character into UTF-8, and prepends it 674 * to the string. 675 * 676 * Params: 677 * wc = a Unicode character 678 * 679 * Returns: @string 680 */ 681 public StringG prependUnichar(dchar wc) 682 { 683 auto __p = g_string_prepend_unichar(gString, wc); 684 685 if(__p is null) 686 { 687 return null; 688 } 689 690 return new StringG(cast(GString*) __p); 691 } 692 693 /** 694 * Replaces the string @find with the string @replace in a #GString up to 695 * @limit times. If the number of instances of @find in the #GString is 696 * less than @limit, all instances are replaced. If @limit is `0`, 697 * all instances of @find are replaced. 698 * 699 * If @find is the empty string, since versions 2.69.1 and 2.68.4 the 700 * replacement will be inserted no more than once per possible position 701 * (beginning of string, end of string and between characters). This did 702 * not work correctly in earlier versions. 703 * 704 * Params: 705 * find = the string to find in @string 706 * replace = the string to insert in place of @find 707 * limit = the maximum instances of @find to replace with @replace, or `0` for 708 * no limit 709 * 710 * Returns: the number of find and replace operations performed. 711 * 712 * Since: 2.68 713 */ 714 public uint replace(string find, string replace, uint limit) 715 { 716 return g_string_replace(gString, Str.toStringz(find), Str.toStringz(replace), limit); 717 } 718 719 /** 720 * Sets the length of a #GString. If the length is less than 721 * the current length, the string will be truncated. If the 722 * length is greater than the current length, the contents 723 * of the newly added area are undefined. (However, as 724 * always, string->str[string->len] will be a nul byte.) 725 * 726 * Params: 727 * len = the new length 728 * 729 * Returns: @string 730 */ 731 public StringG setSize(size_t len) 732 { 733 auto __p = g_string_set_size(gString, len); 734 735 if(__p is null) 736 { 737 return null; 738 } 739 740 return new StringG(cast(GString*) __p); 741 } 742 743 /** 744 * Cuts off the end of the GString, leaving the first @len bytes. 745 * 746 * Params: 747 * len = the new size of @string 748 * 749 * Returns: @string 750 */ 751 public StringG truncate(size_t len) 752 { 753 auto __p = g_string_truncate(gString, len); 754 755 if(__p is null) 756 { 757 return null; 758 } 759 760 return new StringG(cast(GString*) __p); 761 } 762 763 /** 764 * Converts a #GString to uppercase. 765 * 766 * Deprecated: This function uses the locale-specific 767 * toupper() function, which is almost never the right thing. 768 * Use g_string_ascii_up() or g_utf8_strup() instead. 769 * 770 * Returns: @string 771 */ 772 public StringG up() 773 { 774 auto __p = g_string_up(gString); 775 776 if(__p is null) 777 { 778 return null; 779 } 780 781 return new StringG(cast(GString*) __p); 782 } 783 784 /** 785 * Writes a formatted string into a #GString. 786 * This function is similar to g_string_printf() except that 787 * the arguments to the format string are passed as a va_list. 788 * 789 * Params: 790 * format = the string format. See the printf() documentation 791 * args = the parameters to insert into the format string 792 * 793 * Since: 2.14 794 */ 795 public void vprintf(string format, void* args) 796 { 797 g_string_vprintf(gString, Str.toStringz(format), args); 798 } 799 800 /** 801 * Creates a new reference counted string and copies the contents of @str 802 * into it. 803 * 804 * Params: 805 * str = a NUL-terminated string 806 * 807 * Returns: the newly created reference counted string 808 * 809 * Since: 2.58 810 */ 811 public static string refStringNew(string str) 812 { 813 auto retStr = g_ref_string_new(Str.toStringz(str)); 814 815 scope(exit) Str.freeString(retStr); 816 return Str.toString(retStr); 817 } 818 819 /** 820 * Creates a new reference counted string and copies the contents of @str 821 * into it, up to @len bytes. 822 * 823 * Since this function does not stop at nul bytes, it is the caller's 824 * responsibility to ensure that @str has at least @len addressable bytes. 825 * 826 * Params: 827 * str = a string 828 * len = length of @str to use, or -1 if @str is nul-terminated 829 * 830 * Returns: the newly created reference counted string 831 * 832 * Since: 2.58 833 */ 834 public static string refStringNewLen(string str, ptrdiff_t len) 835 { 836 auto retStr = g_ref_string_new_len(Str.toStringz(str), len); 837 838 scope(exit) Str.freeString(retStr); 839 return Str.toString(retStr); 840 } 841 }